//
//  CustomSelect.m
//  WantedAD
//
//  Created by 1 on 13-9-25.
//  Copyright (c) 2013年 liyun. All rights reserved.
//

#import "HCCustomSelect.h"

@implementation HCCustomSelect
@synthesize delegate;

- (id)initWithFrame:(CGRect)frame title:(NSString *)titleStr arr:(NSArray *)passArr
{
    self = [super initWithFrame:frame];
    if (self)
    {
        // Initialization code
        
        NSArray *languages = [NSLocale preferredLanguages];
        NSString *currentLanguage = [languages objectAtIndex:0];
        
        self.backgroundColor = [UIColor clearColor];
        
        UIView *backView2 = [[UIView alloc] initWithFrame:frame];
        backView2.backgroundColor = [UIColor blackColor];
        backView2.alpha = 0.5;
        [self addSubview:backView2];
        
        height = 50*([passArr count]+1);
        
        backView = [[UIView alloc] initWithFrame:CGRectMake(30, (frame.size.height - height)/2, 260, height)];
        backView.backgroundColor = [UIColor whiteColor];
        backView.layer.cornerRadius = 5;
        backView.layer.masksToBounds = YES;
        [self addSubview:backView];
    
        UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(10, 15, 240, 20)];
        title.text = titleStr;
        title.font = HEIGHT_FONT(17.0f);
        title.textColor = SELECT_LINE_TITLE;
        [backView addSubview:title];
        
        UIImageView *line = [[UIImageView alloc] initWithFrame:CGRectMake(10, 48, 240, 2)];
        line.backgroundColor = SELECT_LINE_TITLE;
        [backView addSubview:line];
        
        
        for (int i = 0; i<[passArr count]; i++)
        {
            UILabel *selectLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, i*50+60, 240, 30)];
            selectLabel.text = [passArr objectAtIndex:i];
            selectLabel.font = HEIGHT_FONT(15.0f);
            if(![currentLanguage isEqualToString:@"zh-Hans"])
            {
                selectLabel.numberOfLines = 2;
                selectLabel.font = HEIGHT_FONT(13.0f);
            }
            
            UIImageView *selectLine = [[UIImageView alloc] initWithFrame:CGRectMake(10, i*50+99, 240, 1)];
            selectLine.backgroundColor = TABLE_LINE;
            if(i != [passArr count]-1)
            {
                [backView addSubview:selectLine];
            }
            
            [backView addSubview:selectLabel];
            
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.frame = CGRectMake(0, i*50+60, 200, 50);
            button.tag = i;
            [button addTarget:self action:@selector(buttonTag:) forControlEvents:UIControlEventTouchUpInside];
            [backView addSubview:button];
        }
    }
    return self;
}

- (void)buttonTag:(UIButton *)sender
{
    [delegate selectDelegate:sender.tag];
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch  locationInView:backView];
        
    if((0 < point.x && point.x < 300)  && (50 < point.y && point.y < height))
    {
        [delegate selectDelegate:(int)point.y/50-1];
    }
    else
    {
        [delegate selectDelegate:-1];
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
